package at.ac.tuwien.infosys.aic11.services;

import org.apache.cxf.jaxrs.client.JAXRSClientFactory;

import at.ac.tuwien.infosys.aic11.dto.InternalCustomer;
import at.ac.tuwien.infosys.aic11.dto.Rating;
import at.ac.tuwien.infosys.aic11.dto.Warrantor;
import at.ac.tuwien.infosys.aic11.fault.UnknownCustomerFault;

import javax.faces.bean.ApplicationScoped;
import javax.inject.Named;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Logger;
import javax.ws.rs.WebApplicationException;

@ApplicationScoped
@Named
public class RatingServiceClient implements Serializable {
	private Logger log = Logger.getLogger("RatingServiceClient");
	private static final long serialVersionUID = 1L;

	private List<InternalCustomer> customers;
	private List<Warrantor> warrantors;

	RatingService ratingService = null;

	public RatingServiceClient() {
		ratingService = JAXRSClientFactory.create("http://localhost:9000/", RatingService.class);
	}


	public Rating fetchCustomerRating(String customerName) {
		Rating r = null;
		try {
			r = ratingService.getRating(customerName);
		} catch (WebApplicationException e) {
			log.severe("bad request "+e.getResponse().getStatus());
		}
		return r;
	}


	public List<InternalCustomer> getAllCustomers() {
		if (customers == null) {
			customers = ratingService.getCustomers();
		}
		return customers;
	}

	public List<Warrantor> getWarrantors() {
		if(warrantors == null){
			warrantors = ratingService.getWarrantors();
		}
		return warrantors;
	}

}
